package com.cabrida.livestar.mainstar;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.main.MainActivity;
import com.cabrida.livestar.mainstar.account.AccountActivity;
import com.cabrida.livestar.model.FansStarHomeData;
import com.cabrida.livestar.star.StarAdvanceLvActivity;
import com.cabrida.livestar.star.StarGiftLvActivity;
import com.cabrida.livestar.star.StarGuardLvActivity;
import com.cabrida.livestar.star.StarPlayBackGvActivity;
import com.cabrida.livestar.star.StarRecordActivity;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.view.CircleImageView;

/**
 * 明星端我的
 * 
 * @author why
 */
public class StarMineView extends LinearLayout implements OnClickListener {
	private Context mContext;
	private LinearLayout ll_account;
	private LinearLayout ll_quit;
	private LinearLayout ll_guard;
	private LinearLayout ll_gift;
	private LinearLayout ll_starcoo;
	private LinearLayout ll_record;
	private LinearLayout ll_advance;
	private LinearLayout ll_playback;
	private LinearLayout ll_dynamic;
	
	private CircleImageView iv_header;
	private ImageView iv_bg;
	private TextView tv_sid;
	private TextView tv_bg;
	private TextView tv_name;
	private TextView tv_des1;
	private TextView tv_des2;
	private TextView tv_des3;
	private TextView tv_book;
	private TextView tv_obtain;
	private TextView tv_advance_num;
	private TextView tv_playback_num;
	private TextView tv_dynamic_num;
	private ImageView iv_live_state;
	private ImageView iv_guard_header_1;
	private ImageView iv_guard_header_2;
	private ImageView iv_guard_header_3;
	private ImageView iv_guard_header_4;
	private TextView tv_guard_header_more;
	
	public StarMineView(Context context) {
		super(context);
		mContext = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.view_main_star_mine, this);
		Init(view);
	}

	/**
	 * 初始化控件
	 */
	private void Init(View view) {
		ll_account = (LinearLayout) view.findViewById(R.id.ll_account);
		ll_account.setOnClickListener(this);
		ll_guard = (LinearLayout) view.findViewById(R.id.ll_guard);
		ll_gift = (LinearLayout) view.findViewById(R.id.ll_gift);
		ll_starcoo = (LinearLayout) view.findViewById(R.id.ll_starcoo);
		ll_record = (LinearLayout) view.findViewById(R.id.ll_record);
		ll_advance = (LinearLayout) view.findViewById(R.id.ll_advance);
		ll_playback = (LinearLayout)view. findViewById(R.id.ll_playback);
		ll_dynamic = (LinearLayout) view.findViewById(R.id.ll_dynamic);
		iv_header= (CircleImageView)view. findViewById(R.id.iv_header);
		iv_bg= (ImageView) view.findViewById(R.id.iv_bg);
		tv_sid=(TextView) view.findViewById(R.id.tv_sid);
		tv_bg=(TextView) view.findViewById(R.id.tv_bg);
		 tv_name=(TextView) view.findViewById(R.id.tv_name);
		tv_des1=(TextView) view.findViewById(R.id.tv_des1);
		 tv_des2=(TextView)view. findViewById(R.id.tv_des2);
		 tv_des3=(TextView) view.findViewById(R.id.tv_des3);
		 tv_book=(TextView)view. findViewById(R.id.tv_book);
		 tv_obtain=(TextView) view.findViewById(R.id.tv_obtain);
		 tv_advance_num=(TextView)view. findViewById(R.id.tv_advance_num);
		 tv_playback_num=(TextView)view. findViewById(R.id.tv_playback_num);
		 tv_dynamic_num=(TextView) view.findViewById(R.id.tv_dynamic_num);
		 iv_live_state=(ImageView) view.findViewById(R.id.iv_live_state);
		 iv_guard_header_1=(ImageView)view. findViewById(R.id.iv_guard_header_1);
		 iv_guard_header_2=(ImageView) view.findViewById(R.id.iv_guard_header_2);
		 iv_guard_header_3=(ImageView) view.findViewById(R.id.iv_guard_header_3);
		 iv_guard_header_4=(ImageView)view. findViewById(R.id.iv_guard_header_4);
		 tv_guard_header_more=(TextView) view.findViewById(R.id.tv_guard_header_more);
		ll_quit = (LinearLayout) view.findViewById(R.id.ll_quit);
		ll_quit.setOnClickListener(this);
		
		tv_bg.setOnClickListener(this);
		ll_guard.setOnClickListener(this);
		ll_gift.setOnClickListener(this);
		ll_starcoo.setOnClickListener(this);
		ll_record.setOnClickListener(this);
		ll_advance.setOnClickListener(this);
		ll_playback.setOnClickListener(this);
		ll_dynamic.setOnClickListener(this);
		
		Intent intent = new Intent();
		intent.setAction(MainStarActivity.ACTION_MAINSTAR_ME);
		mContext.sendBroadcast(intent);
	}
	
	/** 主页数据 */
	public void setData(FansStarHomeData starhomedata) {
		tv_sid.setText(starhomedata.getStarData().getS_udid().toString());
		MyUtils.loadImg(starhomedata.getStarData().getU_avatar(), iv_header);
		MyUtils.loadImg(starhomedata.getStarData().getS_backdrop(), iv_bg);
		tv_name.setText(starhomedata.getStarData().getS_name().toString());
		tv_des1.setText(starhomedata.getStarData().getS_musictype().toString());
		tv_book.setText(starhomedata.getStarData().getS_booked().toString());
		tv_obtain.setText(starhomedata.getStarData().getU_obtain().toString());
		tv_advance_num.setText(starhomedata.getAdvance().toString());
		tv_playback_num.setText(starhomedata.getLive().toString());
		tv_dynamic_num.setText(starhomedata.getCircle().toString());
		if (!TextUtils.isEmpty(starhomedata.getLive().toString())&&!"0".equals(starhomedata.getLive().toString())) {
			iv_live_state.setVisibility(View.VISIBLE);
		}else {
			iv_live_state.setVisibility(View.INVISIBLE);
		}
		iv_guard_header_1.setVisibility(View.GONE);
		iv_guard_header_2.setVisibility(View.GONE);
		iv_guard_header_3.setVisibility(View.GONE);
		iv_guard_header_4.setVisibility(View.GONE);
		tv_guard_header_more.setVisibility(View.GONE);
		if (null!=starhomedata.getList()&&starhomedata.getList().size()>0) {
			if (starhomedata.getList().size()>=1) {
				iv_guard_header_1.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(0).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=2) {
				iv_guard_header_2.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(1).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=3) {
				iv_guard_header_3.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(2).getU_avatar(), iv_guard_header_1);
			}
			if (starhomedata.getList().size()>=4) {
				iv_guard_header_4.setVisibility(View.VISIBLE);
				MyUtils.loadImg(starhomedata.getList().get(3).getU_avatar(), iv_guard_header_1);
			}
			String guardNum = starhomedata.getGuardNum().toString();
			if (!TextUtils.isEmpty(guardNum)) {
				int gn= Integer.valueOf(guardNum);
				if (starhomedata.getList().size()>=4&&gn>4) {
					tv_guard_header_more.setText(starhomedata.getGuardNum().toString());
					tv_guard_header_more.setVisibility(View.VISIBLE);
				}
			}
		} 
		
	}
	
	
	
private Dialog dialogQuit;
	
	public void popQuitWindows() {
		showQuitDialog();
	}

	private void showQuitDialog() {
		initQuitDialogView();
		if (null != dialogQuit && dialogQuit.isShowing()) {
			return;
		}
		if (null != dialogQuit) {
			/**
			 * 等数据加载完毕，显示对话框
			 */
			Window window = dialogQuit.getWindow();
			// 设置显示动画
			window.setWindowAnimations(R.style.main_menu_animstyle);
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = 0;
			wl.y = ((Activity) mContext).getWindowManager().getDefaultDisplay()
					.getHeight();
			// 以下这两句是为了保证按钮可以水平满屏
			wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
			wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
			// 设置显示位置
			dialogQuit.onWindowAttributesChanged(wl);
			// 设置点击外围解散
			dialogQuit.setCanceledOnTouchOutside(true);
			dialogQuit.show();
		}
	}

	private void initQuitDialogView() {
		LayoutInflater mInflater = LayoutInflater.from(mContext);
		View view = mInflater.inflate(R.layout.dialog_quit, null);

		Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogQuit) {
					dialogQuit.dismiss();
				}
			}
		});
		
		Button btn_sure = (Button) view.findViewById(R.id.btn_sure);
		btn_sure.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialogQuit) {
					dialogQuit.dismiss();
					Intent intent = new Intent();
					intent.setAction(MainActivity.ACTION_QUIT);
					mContext.sendBroadcast(intent);
				}
			}
		});

		dialogQuit = new Dialog(mContext, R.style.dialogWindowStyle);
		dialogQuit.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialogQuit.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}
	
	public void setUploadPhoto(Bitmap bm){
		iv_bg.setImageBitmap(bm);
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.tv_bg:
			intent = new Intent();
			intent.setAction(MainStarActivity.ACTION_UPLOAD_PHOTO);
			mContext.sendBroadcast(intent);
			break;
		case R.id.ll_account:
			intent = new Intent();
			intent.setClass(mContext, AccountActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_quit:
			popQuitWindows();
			break;
		case R.id.ll_guard:
		{
		Bundle b = new Bundle();
		b.putString("suid", Global.U_id);
		intent = new Intent();
		intent.putExtras(b);
		intent.setClass(mContext, StarGuardLvActivity.class);
		mContext.startActivity(intent);
		}
		break;
		case R.id.ll_gift:
		{
			Bundle b = new Bundle();
			b.putString("suid", Global.U_id);
			intent = new Intent();
			intent.putExtras(b);
			intent.setClass(mContext, StarGiftLvActivity.class);
			mContext.startActivity(intent);
		}
			break;
		case R.id.ll_starcoo:
		{
			intent = new Intent();
			intent.setClass(mContext, StarCooActivity.class);
			mContext.startActivity(intent);
		}
		break;
		case R.id.ll_record:
		{
			Bundle b = new Bundle();
			b.putString("suid", Global.U_id);
			intent = new Intent();
			intent.putExtras(b);
			intent.setClass(mContext, StarRecordActivity.class);
			mContext.startActivity(intent);
		}
			break;
		case R.id.ll_advance:
			intent = new Intent();
			intent.setClass(mContext, StarAdvanceLvActivity.class);
			mContext.startActivity(intent);
			break;
		case R.id.ll_playback:
			intent = new Intent();
			intent.setClass(mContext, StarPlayBackGvActivity.class);
			mContext.startActivity(intent);
			break;
		default:
			break;
		}
	}

}
